<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 银行卡管理 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 列表 -->
		<view class="container-list" v-if="bankCardList.length">
			<view class="list-item" v-for="(item, index) in bankCardList" :key="index" @click="selectBankCard(item)">
				<view class="item-info">
					<view class="info-top flex align-items-start">
						<view class="top-name flex-item flex flex-wrap">
							<text class="text text-ellipsis">{{item.name}}</text>
							<text class="text text-ellipsis">{{item.bank_name}}</text>
						</view>
						<view class="top-operate" :class="{expand: item.openOperate}">
							<image class="operate-btn" src="/static/more.png" mode="aspectFit" @click.stop="changeOperate(item.id, true)"></image>
							<view class="operate-list">
								<view class="list-option delete" @click.stop="handleDelete(item.id)">删除</view>
								<view class="list-option edit" @click.stop="handleEdit(item.id)">编辑</view>
							</view>
							<view class="operate-mask" @click.stop="changeOperate(item.id, false)"></view>
						</view>
					</view>
					<view class="info-number text-ellipsis">{{item.bank_card_number}}</view>
				</view>
				<view class="item-default flex justify-content-between align-items-center">
					<view class="default-label">是否默认银行卡</view>
					<view class="default-radio" :class="{active: item.default_state == 1}" @click.stop="handleSetDefault(item.id)">
						<image src="/static/tick.png" mode="aspectFill" v-if="item.default_state == 1"></image>
					</view>
				</view>
				<view class="item-mask" v-if="bankCardLoading">
					<view class="mask-top">
						<view class="top-left"></view>
						<view class="top-right"></view>
					</view>
					<view class="mask-center">
						<view class="center-box"></view>
					</view>
					<view class="mask-bottom">
						<view class="bottom-left"></view>
						<view class="bottom-right"></view>
					</view>
				</view>
			</view>
		</view>
		<empty top="30%" title="暂无相关内容~" v-else></empty>
		<!-- 底部按钮 -->
		<view class="container-footer" @click="closeOperate()">
			<view class="footer-btn" @click="handleAdd()">添加银行卡</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 银行卡列表
				bankCardList: [],
				// 打开操作弹窗索引
				openOperateIndex: -1,
				// 是否为选择功能
				isSelect: false,
				// 银行卡是否加载
				bankCardLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			uni.showLoading({
				title: "加载中"
			})
			this.isSelect = option.select == 1 ? true : false
			this.getBankCard(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				this.bankCardLoading = true
				this.getBankCard(() => {
					this.bankCardLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.bankCardLoading = true
			this.getBankCard(() => {
				this.bankCardLoading = false
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			// 获取银行卡列表
			getBankCard(fn) {
				this.$util.request("main.bankCard.list").then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.openOperateIndex = -1
						this.bankCardList = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取银行卡列表 ', error)
				})
			},
			// 切换操作选项打开状态
			changeOperate(id, state) {
				const index = this.bankCardList.findIndex(item => item.id == id)
				this.bankCardList[index].openOperate = state
				if (state) this.openOperateIndex = index
				else this.openOperateIndex = -1
				this.$forceUpdate()
			},
			// 关闭操作弹窗
			closeOperate() {
				if (this.openOperateIndex > -1) {
					this.bankCardList[this.openOperateIndex].openOperate = false
					this.openOperateIndex = -1
				}
			},
			// 添加银行卡
			handleAdd() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/public/bank-card/edit",
					isToken: true,
				})
			},
			// 编辑银行卡
			handleEdit(id) {
				this.closeOperate()
				this.$util.toPage({
					mode: 1,
					path: "/pages/public/bank-card/edit?id=" + id,
					isToken: true,
				})
			},
			// 删除银行卡
			handleDelete(id) {
				this.closeOperate()
				uni.showModal({
					content: " 确认删除此银行卡？ \n 点击【确认删除】按钮删除 ",
					confirmText: "确认删除",
					cancelText: "我再想想",
					confirmColor: "#DE2826",
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: "加载中",
								mask: true
							})
							this.$util.request("main.bankCard.delete", {
								id: id
							}).then(res => {
								if (res.code == 1) {
									uni.showToast({
										icon: "success",
										title: "删除成功"
									})
									this.getBankCard()
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							}).catch(error => {
								console.error('删除银行卡 ', error)
							})
						}
					}
				})
			},
			// 设置默认银行卡
			handleSetDefault(id) {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				this.$util.request("main.bankCard.setDefault", {
					id: id
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						this.getBankCard()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('设置默认银行卡 ', error)
				})
			},
			// 选择银行卡
			selectBankCard(item) {
				if (!this.isSelect) return
				let pages = getCurrentPages()
				let prevPage = pages[pages.length - 2]
				prevPage.$vm.currentBankCard = item
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 112rpx;

		.container-list {
			padding: 32rpx;

			.list-item {
				position: relative;
				border-radius: 16rpx;
				background: #FFF;
				margin-top: 32rpx;

				&:first-child {
					margin-top: 0;
				}

				.item-info {
					padding: 32rpx;
					border-bottom: 1rpx solid #F6F7FB;

					.info-top {
						.top-name {
							.text {
								color: #242629;
								font-size: 32rpx;
								font-weight: 600;
								line-height: 44rpx;
								margin-right: 16rpx;
								margin-bottom: 24rpx;
							}
						}

						.top-operate {
							position: relative;

							.operate-btn {
								width: 48rpx;
								height: 48rpx;
							}

							.operate-list {
								display: none;
								position: absolute;
								right: 0;
								top: 68rpx;
								width: 176rpx;
								border-radius: 12rpx;
								background: #FFF;
								box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.10);
								z-index: 89;

								.list-option {
									font-size: 28rpx;
									line-height: 40rpx;
									text-align: center;
									padding: 24rpx;
									border-top: 1rpx solid #F6F7FB;

									&:first-child {
										border-top: none;
									}

									&.delete {
										color: #DE2826;
									}

									&.edit {
										color: #FFBD0A;
									}
								}
							}

							.operate-mask {
								display: none;
								position: fixed;
								top: 0;
								left: 0;
								right: 0;
								bottom: 0;
								z-index: 88;
							}

							&.expand {

								.operate-list,
								.operate-mask {
									display: block;
								}
							}
						}
					}

					.info-number {
						color: #242629;
						font-size: 28rpx;
						line-height: 40rpx;
					}
				}

				.item-default {
					padding: 24rpx 32rpx 32rpx;

					.default-label {
						color: #8D929C;
						font-size: 28rpx;
						line-height: 40rpx;
					}

					.default-radio {
						width: 40rpx;
						height: 40rpx;
						background: #E1E1E1;
						border-radius: 50%;

						&.active {
							background: var(--theme-color);
						}
					}
				}

				.item-mask {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					z-index: 5;
					background: #FFF;
					display: flex;
					flex-direction: column;

					.mask-top {
						display: flex;
						justify-content: space-between;
						padding: 32rpx 32rpx 0;

						.top-left {
							flex: 1;
							height: 44rpx;
							background: #eee;
						}

						.top-right {
							margin-left: 32rpx;
							width: 52rpx;
							height: 44rpx;
							background: #eee;
						}
					}

					.mask-center {
						margin-top: 24rpx;
						padding: 0 32rpx;

						.center-box {
							width: 100%;
							height: 40rpx;
							background: #eee;
						}
					}

					.mask-bottom {
						display: flex;
						justify-content: space-between;
						margin-top: 32rpx;
						padding: 24rpx 32rpx 32rpx;
						border-top: 1rpx solid #F6F7FB;

						.bottom-left {
							width: 58%;
							height: 40rpx;
							background: #eee;
						}

						.bottom-right {
							margin-left: 32rpx;
							flex: 1;
							height: 40rpx;
							background: #eee;
						}
					}
				}
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;
			padding: 16rpx 24rpx;

			.footer-btn {
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				background: var(--theme-color);
				text-align: center;
			}
		}
	}
</style>